package com.fire.web.modules.all.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.fire.common.mybatis.mvc.wrapper.EntityWrapper;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.entity.FireProprietorModel;
import com.fire.web.modules.all.request.AddProprietorReq;
import com.fire.web.modules.all.service.IFireProprietorService;
import com.fire.web.modules.all.service.ISequenceService;
import com.fire.web.modules.all.service.ITMitErrorCodeService;
import com.fire.web.modules.all.utils.TokenUtil;
import com.fire.web.modules.sso.service.IOAuthService;
import com.fire.web.security.shiro.realm.UserRealm;
import com.fire.web.utils.PageRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fire.web.modules.all.utils.WrapperUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@RestController
@RequestMapping("/proprietor")
public class ProprietorController extends CrudController<IFireProprietorService, FireProprietorModel> {
	private static final Logger LOGGER = LoggerFactory.getLogger(ProprietorController.class);

	@Autowired
	ITMitErrorCodeService errorCodeService;
	@Autowired
	IFireProprietorService fireProprietorService;
	@Autowired
	ISequenceService sequenceService;
	@Autowired
	IOAuthService oAuthService;

	/**
	 * 查询列表
	 *
	 * @return
	 */
	@GetMapping("/getProprietorList")
	public AjaxResult getProprietorList(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult ajaxResult;
		try {
			String token = TokenUtil.getToken(request, response);
			UserRealm.Principal principalByAccessToken = oAuthService.getPrincipalByAccessToken(token);
			String id = principalByAccessToken.getId();
			String username = principalByAccessToken.getUsername();

			//遍历参数获得查询条件
			EntityWrapper ew = WrapperUtil.getEw(params);
			//查询
			Page pageBean = null;
			String adminStr = "admin";
			if(adminStr.equals(username)){
				pageBean = fireProprietorService.selectPage(PageRequest.getPage(), ew);
			} else {
				pageBean = fireProprietorService.selectPage(PageRequest.getPage(), ew.eq("adminid", id));
			}
			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "获取成功", pageBean, "获取成功");
		} catch (Exception e) {
			LOGGER.error("CrudController.page().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);

		return ajaxResult;
	}

	/**
	 * 增加业主单位
	 *
	 * @return
	 */
	@PostMapping("/addProprietor")
	@ResponseBody
	public AjaxResult addProprietor(AddProprietorReq req, HttpServletRequest request, HttpServletResponse response) {
		LOGGER.info("==================新增业主单位开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			String token = TokenUtil.getToken(request, response);
			UserRealm.Principal principalByAccessToken = oAuthService.getPrincipalByAccessToken(token);
			String id = principalByAccessToken.getId();

			ajaxResult = fireProprietorService.addProprietor(req, id);
		} catch (Exception e) {
			LOGGER.error("ProprietorController.addProprietor().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================新增业主单位结束==================");
		return ajaxResult;
	}

	/**
	 * 更新业主单位
	 *
	 * @return
	 */
	@PutMapping("/updateProprietor")
	@ResponseBody
	public AjaxResult updateProprietor(AddProprietorReq req) {
		LOGGER.info("==================更新业主单位开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			ajaxResult = fireProprietorService.updateProprietor(req);
		} catch (Exception e) {
			LOGGER.error("ProprietorController.updateProprietor().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================更新业主单位结束==================");
		return ajaxResult;
	}

	/**
	 * 查询业主单位下拉选
	 *
	 * @return
	 */
	@PostMapping("/getProprietorItem")
	@ResponseBody
	public AjaxResult getProprietorItem(AddProprietorReq req) {
		LOGGER.info("==================查询业主单位下拉选开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			ajaxResult = fireProprietorService.getProprietorItem();
		} catch (Exception e) {
			LOGGER.error("ProprietorController.getProprietorItem().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================查询业主单位下拉选结束==================");
		return ajaxResult;
	}

}
